<?php

namespace app\admin\controller\auth;

use app\common\controller\AdminController;
use clt\Leftnav;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use fast\Tree;

/**
 * @ControllerAnnotation(title="auth_group")
 */
class Group extends AdminController
{

    /**
     * 允许修改的字段
     * @var array
     */
    protected $allowModifyFields = [
        'max_customers_num'
    ];
    protected $sort = [
        'id'   => 'DESC',
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new \app\admin\model\AuthGroup();
        
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model->field('id,pid,title,create_time,max_customers_num')
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select()->toArray();
            $nav = new Leftnav();
            $list = $nav->treelst($list);
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [
                'title|用户组名'  => 'require|unique:AuthGroup',
            ];
            $this->validate($post, $rule);
            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败:'.$e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [
                'title|用户组名'  => 'require|unique:AuthGroup',
            ];
            Tree::instance()->init($this->model->where('status','=',1)->select()->toArray());
            // 父节点不能是它自身的子节点或自己本身
            if (in_array($post['pid'], Tree::instance()->getChildrenIds($row->id, true))) {
                $this->error('父级不能是它的子级和自身');
            }
            $this->validate($post, $rule);
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('row', $row);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="删除")
     */
    public function delete($id)
    {
        $this->checkPostRequest();
        $row = $this->model->whereIn('id', $id)->select();
        $row->isEmpty() && $this->error('数据不存在');
        try {
            $son_id=\think\facade\Db::name('auth_group')->where('pid','=',$id)->value('id');
            if($son_id){
                throw new \Exception('当前角色存在下级角色！', 0);
            }
            $save = $row->delete();
        } catch (\Exception $e) {
            $this->error('删除失败：'.$e->getMessage());
        }
        $save ? $this->success('删除成功') : $this->error('删除失败');
    }

    
}